var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    tabClass: ["", "", "", "", ""],
    UserRankInfo: {},
    PayCreditName: "",
    orderStatistics: {},
    supportCompany: "",
    supportPhone: "",
    PayCredits: 0,
    Distributor: true,
    isAccredit: ''
  },

  tofenxiao: function () {
    var mobile = this.data.userinfo.Mobile;
    if (mobile) {
      wx.navigateTo({
        url: '/fenxiao/index/index',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: "请先完善会员手机号！",
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '/pages/editor/index',
          })
        }
      })
    }
  },

  toOrder: function () {
    wx.navigateTo({
      url: '/pages/order-list/index',
    })
  },
  toOrderStatus: function (e) {
    let status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '/pages/order-list/index?t=' + status,
    })
  },
  toRecores: function () {
    wx.navigateTo({
      url: '/fenxiao/records/index',
    })
  },
  toMyteams: function () {
    wx.navigateTo({
      url: '/fenxiao/myteam/index',
    })
  },
  toTixian: function () {
    wx.navigateTo({
      url: '/fenxiao/tixian/index',
    })
  },
  toRecharge: function () {
    wx.navigateTo({
      url: '/pages/recharge/index',
    })
  },
  toAppiont: function () {
    wx.navigateTo({
      url: '/pages/yuyue-list/index',
    })
  },
  toJifen: function () {
    let PayCreditName = this.data.PayCreditName;
    let PayCredits = this.data.PayCredits;
    wx.navigateTo({
      url: '/pages/jifen/index?PayCreditName=' + PayCreditName + '&PayCredits=' + PayCredits,
    })
  },
  toCoupon: function () {
    wx.navigateTo({
      url: '/pages/member/coupon/index',
    })
  },
  toFavorite: function () {
    wx.navigateTo({
      url: '/pages/member/Favorite/index',
    })
  },
  toAddress: function () {
    wx.navigateTo({
      url: '/pages/select-address/index?issel=0',
    })
  },
  toCallus: function () {
    wx.navigateTo({
      url: '/pages/kefu/index',
    })
  },
  toEvaluate: function () {
    wx.navigateTo({
      url: '/pages/mypingjia/index',
    })
  },
  // 授权登陆
  toLogin: function () {
    let back = '/pages/member/index'
    wx.redirectTo({
      url: '../accredit/index?back=' + back,
    })
  },
  //是否授权登陆
  toShowToast: function () {
    let back = '/pages/member/index'
    wx.showModal({
      title: '提示',
      content: '未登录不可查看，是否确认登陆',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../accredit/index?back=' + back,
          })
        }
      }
    })
  },

  //判断是否开通分销功能
  checkDistributor: function () {
    var that = this
    wx.request({
      url: app.globalData.host + '/api/Store/CheckDistributor?storeId=' + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            Distributor: res.data.Data
          })
        }
      },
    })
  },
  toShareCode: function () {
    var mobile = this.data.userinfo.Mobile;
    if (mobile) {
      wx.navigateTo({
        url: '/fenxiao/shareCodes/index?userid=' + app.globalData.userInfo.Uid,
      })
    } else {
      wx.showModal({
        title: '提示',
        content: "请先完善会员手机号！",
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '/pages/editor/index',
          })
        }
      })
    }
  },
  //编辑信息
  editorUser: function () {
    wx.navigateTo({
      url: '/pages/editor/index',
    })
  },
  /**
   * 获取店铺信息
   */
  GetVideoInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Store/GetInfo?id=' + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          that.data.logoSrc = 'https://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/banner/' + res.data.Data.Banner;
          that.data.supportPhone = res.data.Data.SupportPhone;
          that.setData({
            videoSrc: res.data.Data.VideoSrc,
            supportCompany: res.data.Data.SupportCompany
          })
        }
      }
    })
  },
  //拨打服务商电话
  callCompany: function () {
    var that = this;
    if (that.data.supportPhone == '') {
      return
    } else {
      wx.makePhoneCall({
        phoneNumber: that.data.supportPhone
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
 
    that.getOrderStatistics();
    that.GetVideoInfo();
    that.checkDistributor();
    //获取积分名称
    wx.request({
      url: app.globalData.host + '/api/Store/GetStoreCreditConfigInfo?storeId=' + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data.PayCreditName != '') {
            that.setData({
              PayCreditName: res.data.Data.PayCreditName
            })
          }
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.errMsg,
            showCancel: false
          })
          return;
        }
      }
    })
  },

  //获取用户信息
  GetUserInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/User/GetUserInfo?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          var UserRankInfo = res.data.Data.UserRankInfo
          UserRankInfo.Avatar = 'https://static.711688.net.cn' + '/upload/userrank/source/' + UserRankInfo.Avatar
          that.setData({
            UserRankInfo: UserRankInfo,
            PayCredits: res.data.Data.UserInfo.PayCredits,
            userinfo: res.data.Data.UserInfo
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.errMsg,
            showCancel: false
          })
          return;
        }
      }
    })
  },
  getOrderStatistics: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/order/statistics',
      data: { uid: app.globalData.userInfo.Uid },
      success: (res) => {
        if (res.data.Code == 0) {
          var tabClass = that.data.tabClass;
          if (res.data.Data.count_id_no_pay > 0) {
            tabClass[1] = "circle"
          }
          if (res.data.Data.count_id_no_transfer > 0) {
            tabClass[2] = "circle"
          }
          if (res.data.Data.count_id_no_confirm > 0) {
            tabClass[3] = "circle"
          }
          if (res.data.Data.count_id_success > 0) {
            tabClass[4] = "circle"
          }

          that.setData({
            tabClass: tabClass,
            orderStatistics: res.data.Data
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // util.authori('/pages/authori/index');
    let that = this;
    if(app.globalData.userInfo){
      util.isAccredit(function (isAccredit) {
        that.setData({
          isAccredit: isAccredit
        })
      });
      that.getOrderStatistics();
      that.GetUserInfo();
      that.setData({
        userinfo: app.globalData.userInfo
      })
    }else{
      setTimeout(function(){
        that.onShow();
      },1000)
    }
   
  
  },


  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  },
})